package com.brotherly.util;

import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.Hashtable;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Point;
import android.util.SparseArray;
import android.view.MotionEvent;

public class BitmapUtil
{

    public static int getPointerId(MotionEvent event, int pindex)
    {
        return event.getPointerId(pindex);
    }

    public static float getX(MotionEvent event, int pindex)
    {
        return event.getX(pindex);
    }

    public static float getY(MotionEvent event, int pindex)
    {
        return event.getY(pindex);
    }

    public static Context context = null;

    private static HashMap<String, WeakReference<Bitmap>> ExtBitmapCaches = new HashMap<String, WeakReference<Bitmap>>();

    private static SparseArray<WeakReference<Bitmap>> BitmapCaches = new SparseArray<WeakReference<Bitmap>>();

    public synchronized static Bitmap loadBitmap(Resources resources, int resid)
    {
        WeakReference<Bitmap> wr = BitmapCaches.get(resid);
        if (wr == null || wr.get() == null)
        {
            Bitmap bp = BitmapFactory.decodeResource(resources, resid);
            wr = new WeakReference<Bitmap>(bp);
            BitmapCaches.append(resid, wr);
        }
        return wr.get();
    }

    /**
     * 加载图片，预防重复加载
     * 
     * @param path
     * @return
     */
    public synchronized static Bitmap loadBitmap(String path)
    {
        Bitmap bitmap = null;
        if (ExtBitmapCaches.containsKey(path))
        {
            bitmap = ExtBitmapCaches.get(path).get();
        }
        if (bitmap == null)
        {
            bitmap = BitmapFactory.decodeFile(path);
            if (bitmap != null)
            {
                ExtBitmapCaches.put(path, new WeakReference<Bitmap>(bitmap));
            }
        }
        return bitmap;
    }

    /**
     * 旋转绘图
     * 
     * @param canvas
     * @param paint
     * @param bitmap
     * @param srcCenter
     * @param desPos
     * @param rotateDegree
     * @param scale
     */
    public static void drawBitmap(Canvas canvas, Paint paint, Bitmap bitmap, Point srcCenter, Point desPos, float rotateDegree,
            float scale)
    {
        if (rotateDegree > 0 || scale != 1.0f)
        {
            Matrix matrix = new Matrix();
            matrix.postTranslate(-srcCenter.x, -srcCenter.y);
            matrix.postRotate(rotateDegree);
            matrix.postTranslate(srcCenter.x, srcCenter.y);
            matrix.postScale(scale, scale);
            matrix.postTranslate(desPos.x - srcCenter.x * scale, desPos.y - srcCenter.y * scale);
            canvas.drawBitmap(bitmap, matrix, paint);
        } else
        {
            canvas.drawBitmap(bitmap, desPos.x - srcCenter.x * scale, desPos.y - srcCenter.y * scale, paint);
        }
    }

    private static Hashtable<String, String> citys = null;

    /**
     * 获取城市的拼音
     * 
     * @param chineseWord
     * @return
     */
    public static String getCityPingYin(String chineseWord)
    {
        if (chineseWord == null || chineseWord.equals(""))
        {
            return "";
        }

        if (citys == null)
        {
            citys = new Hashtable<String, String>();
            citys.put("北京", "beijing");
            citys.put("上海", "shanghai");
            citys.put("沈阳", "shenyang");
            citys.put("南京", "nanjing");
            citys.put("深圳", "shenzhen");
            citys.put("重庆", "chongqing");
            citys.put("大连", "dalian");
            citys.put("宁波", "ningbo");
            citys.put("天津", "tianjin");
            citys.put("常州", "changzhou");
            citys.put("武汉", "wuhan");
            citys.put("太原", "taiyuan");
            citys.put("成都", "chengdu");
            citys.put("广州", "changzhou");
            citys.put("长沙", "changsha");
            citys.put("西安", "xian");
            citys.put("福州", "fuzhou");
            citys.put("青岛", "qingdao");
            citys.put("杭州", "hangzhou");
            citys.put("佛山", "foshan");
            citys.put("东莞", "dongguan");
            citys.put("泉州", "quanzhou");
            citys.put("台州", "taizhou");
            citys.put("无锡", "wuxi");
            citys.put("珠海", "zhuhai");
            citys.put("苏州", "suzhou");
            citys.put("漳州", "zhangzhou");
            citys.put("厦门", "xiamen");
            citys.put("三明", "sanming");
            citys.put("莆田", "putian");
            citys.put("宁德", "ningde");
            citys.put("龙岩", "longyan");
            citys.put("南昌", "nanchang");
            citys.put("南平", "nanping");
            citys.put("石家庄", "shijiazhuang");
            citys.put("呼和浩特", "huhehaote");
        }

        String key = chineseWord;
        if (key.endsWith("市"))
        {
            key = key.substring(0, key.length() - 1);
        }
        if (citys.containsKey(key))
        {
            return citys.get(key);
        }
        return chineseWord;
    }

    /**
     * 去除城市名后边的“市”
     * 
     * @param city
     * @return
     */
    public static String filterCity(String city)
    {
        if (city == null)
            return city;

        if (city.endsWith("市"))
        {
            city = city.substring(0, city.length() - 1);
        }
        return city;
    }

    public static boolean EqualsCity(String city1, String city2)
    {
        return filterCity(city1).equals(filterCity(city2));
    }
}
